﻿using System;
using System.Windows.Input;
using PDFDesign.Properties;
using PDFDesign.Command;
using ICSharpCode.AvalonEdit;
using PDFDesign.Common;
using PDFDesign.Model;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.Linq;

namespace PDFDesign.ViewModel
{
    public class MockDataViewModel : BaseViewModel
    {
        private TextEditor textEditor;

        public MockDataViewModel(TextEditor textEditor)
        {
            this.textEditor = textEditor;
            var dataList = DesignUtils.GetSaveDataList();
            DataList = new ObservableCollection<SaveData>(dataList);
        }

        private SaveData currentData;
        /// <summary>
        /// 当前数据
        /// </summary>
        public SaveData CurrentData
        {
            get => currentData;
            set
            {
                currentData = value;
                OnPropertyChanged("CurrentData");
                if (currentData == null) return;
                textEditor.Text = Utils.Format(currentData.Data, true, out bool _);
            }
        }

        private ObservableCollection<SaveData> dataList = new ObservableCollection<SaveData>();
        /// <summary>
        /// 数据列表
        /// </summary>
        public ObservableCollection<SaveData> DataList
        {
            get => dataList;
            set
            {
                dataList = value;
                OnPropertyChanged("DataList");
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        private void Update()
        {
            Settings.Default.DataList = JsonConvert.SerializeObject(DataList.ToList());
            Settings.Default.Save();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public ICommand SaveCommand { get => new AnotherCommand(_ => Save()); }
        private void Save()
        {
            object obj = null;
            try
            {
                obj = JsonConvert.DeserializeObject(textEditor.Text);
                if (obj == null)
                {
                    ShowMessage("数据格式错误");
                    return;
                }
            }
            catch (Exception ex)
            {
                ShowMessage($"数据格式错误：{ex.Message}");
                return;
            }
            CurrentData.Data = JsonConvert.SerializeObject(obj);
            Update();
            DialogResult = true;
        }

        private bool? dialogResult;
        public bool? DialogResult
        {
            get => dialogResult;
            set
            {
                dialogResult = value;
                OnPropertyChanged("DialogResult");
            }
        }

        /// <summary>
        /// 格式化
        /// </summary>
        public ICommand FormatCommand => new AnotherCommand(_ => textEditor.Text = Utils.Format(textEditor.Text, true, out bool _));

        /// <summary>
        /// 添加数据
        /// </summary>
        public ICommand AddDataCommand => new AnotherCommand(async _ =>
         {
             var name = await ShowInputDialog("PDF样式设计工具", "请输入模板名称");
             if (string.IsNullOrWhiteSpace(name)) return;
             var saveData = new SaveData { Name = name, Data = "" };
             DataList.Add(saveData);
             CurrentData = saveData;
             Update();
         });

        /// <summary>
        /// 删除数据
        /// </summary>
        public ICommand DelDataCommand => new AnotherCommand(_ =>
        {
            if (CurrentData == null)
            {
                ShowMessage("请先选择数据");
                return;
            }
            DataList.Remove(CurrentData);
            CurrentData = null;
            Update();
        });
    }
}
